/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Symbol Matching.
 *
 * Symbol Matching is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * Symbol Matching is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Symbol Matching.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.reaktiopeli.game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import fi.honkalampisaatio.game.utilities.MediaLoader;


public class TargetButton extends JButton {

	private static final long serialVersionUID = -7865214595068388525L;

	private enum STATE {ON, OFF, HIT};
	private STATE state;
	private final int ICON_HEIGHT = 80;
	private final int MAX_IMAGE_WIDTH = 800;
	private final int MAX_IMAGE_HEIGHT = 800;
	private BufferedImage origImage;
	private ArrayList<MediaElement> questionList;
	private MediaElement answer;
	private int id;
	private Color bgColor = null;

	private Random rnd = new Random();
	
	public TargetButton(int id) {
		this.setName(Integer.toString(id));
		state = STATE.OFF;
		this.id = id;
		questionList = new ArrayList<MediaElement>();
		answer = new MediaElement();
	}

	public void setIconAndImage(String imagepath, String bgColor) {
		if(imagepath!=null && !imagepath.isEmpty()) {
			this.setName(imagepath);
		}
		else if(bgColor!=null && !bgColor.isEmpty()) {
			this.setName(bgColor);
		}

		
		ImageIcon btnIcon = null;
		if(bgColor!=null)
		{
			try {
				this.bgColor = Color.decode(bgColor);
			} catch (Exception e) {
				System.err.println("Button " + imagepath + " color " + bgColor +
						" decoding failed");
			}
		}
		BufferedImage img = null;
		if(imagepath!=null)
		{
			try {
				img = MediaLoader.createImage(imagepath, MAX_IMAGE_HEIGHT, MAX_IMAGE_WIDTH);
			} catch (Exception e) {
				System.err.println("Button " + imagepath + " color " + bgColor +
						" creating image failed");
			}
		}
		try {
			origImage = new BufferedImage( MAX_IMAGE_HEIGHT, MAX_IMAGE_WIDTH, BufferedImage.TYPE_INT_ARGB);
			Graphics2D g = (Graphics2D) origImage.getGraphics();
			if(this.bgColor!=null)
			{
				g.setColor(this.bgColor);
				g.fillRect(0, 0, MAX_IMAGE_HEIGHT, MAX_IMAGE_WIDTH);
			}
			if(img!=null)
			{
				g.drawImage(img, 0, 0, MAX_IMAGE_HEIGHT, MAX_IMAGE_WIDTH, null);
				btnIcon = new ImageIcon(img.getScaledInstance(-1, ICON_HEIGHT, 
		        		Image.SCALE_FAST));
			}
			g.dispose();
		} catch (Exception e) {
			e.printStackTrace();
	        btnIcon = null;
		}
		if(btnIcon!=null) {
			this.setIcon(btnIcon);
		}
	}
	
	public int getId() {
		return id;
	}

	public boolean isOn() {
		return state==STATE.ON;
	}

	public boolean isHit() {
		return state==STATE.HIT;
	}

	public void hitTarget() {
		state = STATE.HIT;
	}

	public void setOn() {
		state = STATE.ON;
	}


	public void setOff() {
		state = STATE.OFF;
	}

	public MediaElement getQuestion() {
		if(questionList==null || questionList.isEmpty()) {
			return null;
		}
		else {
			return questionList.get(rnd.nextInt(questionList.size()));
		}
	}

	public void setQuestions(ArrayList<MediaElement> questionList) {
		this.questionList = questionList;
	}

	public void setAnswer(MediaElement answerElement) {
		answer = answerElement;
	}

	public MediaElement getAnswer() {
		return answer;
	}

	public BufferedImage getOriginalImage() {
		return origImage;
	}

	public Color getBgColor() {
		return bgColor;
	}


}
